<?php
class DBConnector {
	const ID = "id";
	
	private static $m_connection = FALSE;
	
	public static function connect()
	{
		self::$m_connection = mysqli_connect(
			ML_DATABASE_HOST, ML_DATABASE_USERNAME, ML_DATABASE_PASSWORD, ML_DATABASE_NAME);
		if (!self::$m_connection) {
			$error_message = "DBConnector - Failed to connect : ".mysqli_connect_error();
			Log::error($error_message);
			Debug::put($error_message);
			exit();
		}
	}
	
	public static function update($table, $data)
	{
		$size = count($data);
		$index = 0;
		
		if (isset($data[self::ID])) {
			$query = "UPDATE $table SET ";
			foreach ($data as $key => $value) {
				$index++;
				$query .= "$key = '$value'";
				if ($index < $size)
					$query .=", ";
			}
			$query .= " WHERE ".self::ID." = ".$data[self::ID];
		}
		else {
			$query = "INSERT INTO $table (";
			$values ="";
			foreach ($data as $key => $value) {
				$index++;
				$query .= "$key";
				$values .= "'$value'";
				if ($index < $size) {
					$query .= ", ";
					$values .= ", ";
				}
			}
			$query .= ") VALUES ($values)";
		}
		
		Log::info("Query : $query");
		$result = mysqli_query(self::$m_connection, $query);
		if ($result === FALSE)
			Log::error("DBConnector - Failed to update : ".mysqli_error(self::$m_connection));
		return $result;
	}
	
	public static function get_list(
	$table, $start, $end, $search_fields, $keyword, $order, $fields = FALSE, $join = FALSE, $where = FALSE)
	{
		$columns = "*";
		if ($fields !== FALSE) $columns = implode(", ", $fields);
		$query = "SELECT $columns FROM $table";
		if ($join !== FALSE) $query .= " $join";
		
		if (($keyword !== FALSE and !empty($keyword)) or $where !== FALSE) {
			$query .= " WHERE";
		}
		
		if ($where !== FALSE) $query .= " $where";
		
		if ($keyword !== FALSE and !empty($keyword)) {
			$size = count($search_fields);
			$index = 0;
			foreach ($search_fields as $search_field) {
				$index++;
				$query .= " $search_field LIKE '%$keyword%'";
				if ($index < $size) $query .= " OR";
			}
		}
		
		if ($order !== FALSE and !empty($order)) {
			$query .= " ORDER BY $order";
		}
		
		if ($start !== FALSE and $end !== FALSE and $end >= $start) {
			$count = $end - $start;
			$query .= " LIMIT $start, $count";
		}
		
		Log::info("Query : $query");
		$result = mysqli_query(self::$m_connection, $query);
		if ($result === FALSE) {
			Log::error("DBConnector - Failed to get list : ".mysqli_error(self::$m_connection));
			return $result;
		}
		else return $result->fetch_all(MYSQLI_ASSOC);
	}
	
	public static function get($table, $key, $value, $fields = FALSE, $join = FALSE)
	{
		$columns = "*";
		if ($fields !== FALSE) $columns = implode(", ", $fields);
		$query = "SELECT $columns FROM $table";
		if ($join !== FALSE) $query .= " $join";
		
		$query .= " WHERE $key = '$value' LIMIT 1";
		
		Log::info("Query : $query");
		$result = mysqli_query(self::$m_connection, $query);
		if ($result === FALSE) {
			Log::error("DBConnector - Failed to get : ".mysqli_error(self::$m_connection));
			return $result;
		}
		else return $result->fetch_array(MYSQLI_ASSOC);
	}
	
	public static function query($query)
	{
		Log::info("Query : $query");
		$result = mysqli_query(self::$m_connection, $query);
		if ($result === FALSE) {
			Log::error("DBConnector - Failed to query : ".mysqli_error(self::$m_connection));
			return $result;
		}
		else return $result->fetch_all(MYSQLI_ASSOC);
	}
	
	public static function delete($table, $id_list)
	{
		$success = TRUE;
		foreach ($id_list as $id) {
			$query = "DELETE FROM $table WHERE ".self::ID." = $id";
			
			Log::info("Query : $query");
			$result = mysqli_query(self::$m_connection, $query);
			if ($result === FALSE) {
				Log::error("DBConnector - Failed to delete : ".mysqli_error(self::$m_connection));
				$success = FALSE;
			}
		}
		return $success;
	}
	
	public static function close()
	{
		mysqli_close(self::$m_connection);
	}
}